package com.projectName.www.service;

import com.projectName.www.po.shop;
import com.projectName.www.dao.S_J_insertshop_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author 86132
 *
 */
public class S_J_insertshop extends JInternalFrame {
    private JTextField textField;
    private JTextField textField_1;
    public static  shop s;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {


        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    S_J_insertshop frame = new S_J_insertshop();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    /**
     * Create the frame.
     */
    public S_J_insertshop() {

        setTitle("创建店铺");
        setResizable(true);
        setMaximizable(true);
        setClosable(true);
        setBounds(100, 100, 450, 300);

        textField = new JTextField();
        textField.setText("");
        textField.setColumns(10);

        JLabel lblNewLabel = new JLabel("店铺的名字：");

        JLabel lblNewLabel_1 = new JLabel("店铺的描述：");

        textField_1 = new JTextField();
        textField_1.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("温馨提示：一个商家只能创建一个店铺");

        JButton btnNewButton = new JButton("创建");


        JButton btnNewButton_1 = new JButton("取消");

        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(35)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18)
                                                                .addComponent(textField_1))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(lblNewLabel)
                                                                .addGap(18)
                                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 233, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, 213, GroupLayout.PREFERRED_SIZE)))))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(58)
                                                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                                                .addGap(60)
                                                .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(82, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(17)
                                .addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel))
                                .addGap(18)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
                                .addGap(25))

        );
        getContentPane().setLayout(groupLayout);

        btnNewButton.addActionListener(e-> {
                //登录
                try {
                    insert(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });


        btnNewButton_1.addActionListener(e -> {

            try {
                close(e);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });}
        private void insert(ActionEvent e) throws Exception {

            String name = textField.getText();
            String description = textField_1.getText();
            String shopkeeper = OpenBusiness.username;

            s = new shop(name, description, shopkeeper);

            try {

                if (S_J_insertshop_dao.insertshop(s)) {
                    JOptionPane.showMessageDialog(null, "创建成功");
                    return;
                } else {
                    JOptionPane.showMessageDialog(null, "创建失败：店铺已存在或者名字为空");
                }
                return;

            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    private void close(ActionEvent e) throws Exception{
        textField.setText("");
        textField_1.setText("");
    }
}











